<?php
namespace app\demo_record\controller;

use app\demo_record\widget\FormWidget;
use cmf\controller\HomeBaseController;
use think\Exception;
use app\demo_record\model\ConfigTableModel;
use app\demo_record\model\ConfigFileModel;

class ConfigController extends HomeBaseController
{
    protected function loadConfig()
    {
        $options=[
            ["name"=>"0","title"=>"零"],
            ["name"=>"1","title"=>"壹"],
            ["name"=>"2","title"=>"贰"],
        ];
        $location=[
            "text"=>"河北省张家口市涿鹿县",
            "longitude"=>115.060997,
            "latitude"=>39.79802,
        ];
        $config=[
            //字符串
            ["name"=>"text",    "title"=>"文字",    "type"=>"text",     "value"=>"你好",                "describe"=>"请输入文字",          "readable"=>true,"writable"=>true,"required"=>true,"regexp"=>""],
            ["name"=>"color",   "title"=>"颜色",    "type"=>"color",    "value"=>"#b8d979",             "describe"=>"请选择颜色",          "readable"=>true,"writable"=>true,"required"=>true,"regexp"=>""],
            ["name"=>"mobile",  "title"=>"手机号",  "type"=>"mobile",   "value"=>"18354138838",         "describe"=>"请输入手机号",        "readable"=>true,"writable"=>true,"required"=>true,"regexp"=>""],
            ["name"=>"email",   "title"=>"电子邮箱","type"=>"email",    "value"=>"1620428040@qq.com",   "describe"=>"请输入电子邮箱地址",   "readable"=>true,"writable"=>true,"required"=>true,"regexp"=>""],
            ["name"=>"url",     "title"=>"链接",    "type"=>"url",      "value"=>"http://www.baidu.com","describe"=>"请输入URL链接",       "readable"=>true,"writable"=>true,"required"=>true,"regexp"=>""],
            ["name"=>"password","title"=>"密码",    "type"=>"password", "value"=>"123456",              "describe"=>"请输入密码",          "readable"=>true,"writable"=>true,"required"=>true,"regexp"=>""],
            ["name"=>"search",  "title"=>"搜索",    "type"=>"search",   "value"=>"2333",                "describe"=>"请输入要搜索的内容",   "readable"=>true,"writable"=>true,"required"=>true,"regexp"=>""],
            //数值
            ["name"=>"number",  "title"=>"数字",    "type"=>"number",   "value"=>50,                   "describe"=>"请输入数字",          "readable"=>true,"writable"=>true,"required"=>true],
            ["name"=>"range",   "title"=>"范围",    "type"=>"range",    "value"=>50,                   "describe"=>"请选择范围",          "readable"=>true,"writable"=>true,"required"=>true,"min"=>1,"max"=>999],
            //时间
            ["name"=>"date",    "title"=>"日期",    "type"=>"date",     "value"=>0,                    "describe"=>"请输入日期",          "readable"=>true,"writable"=>true,"required"=>true,"format"=>"Y-m-d"],
            ["name"=>"time",    "title"=>"时间",    "type"=>"time",     "value"=>0,                    "describe"=>"请输入时间",          "readable"=>true,"writable"=>true,"required"=>true,"format"=>"H:i:s"],
            ["name"=>"datetime","title"=>"日期时间", "type"=>"datetime", "value"=>0,                   "describe"=>"请输入日期和时间",     "readable"=>true,"writable"=>true,"required"=>true,"format"=>"Y-m-d H:i"],
            //长文本
            ["name"=>"textarea","title"=>"多行文字", "type"=>"textarea", "value"=>"hello world !",     "describe"=>"请输入多行文字",      "readable"=>true,"writable"=>true,"required"=>true],
            ["name"=>"html",    "title"=>"HTML内容", "type"=>"html",     "value"=>"<div>2333</div>",   "describe"=>"请输入HTML格式的内容","readable"=>true,"writable"=>true,"required"=>true],
            //选择
            ["name"=>"checkbox","title"=>"复选",    "type"=>"checkbox", "value"=>true,                 "describe"=>"请选择",              "readable"=>true,"writable"=>true,"required"=>true],
            ["name"=>"select",  "title"=>"下拉",    "type"=>"select",   "value"=>"1",                  "describe"=>"请选择",              "readable"=>true,"writable"=>true,"required"=>true,"options"=>$options],
            ["name"=>"radio",   "title"=>"单选",    "type"=>"radio",    "value"=>"2",                  "describe"=>"请选择",              "readable"=>true,"writable"=>true,"required"=>true,"options"=>$options],
            ["name"=>"multiple","title"=>"多选",    "type"=>"multiple", "value"=>["0","2"],            "describe"=>"请选择",              "readable"=>true,"writable"=>true,"required"=>true,"options"=>$options],
            //对象
            ["name"=>"location","title"=>"地址",    "type"=>"location",  "value"=>$location,           "describe"=>"请选择地址",          "readable"=>true,"writable"=>true,"required"=>true],
            ["name"=>"files",   "title"=>"多个文件", "type"=>"files",    "value"=>[],                  "describe"=>"请上传文件",          "readable"=>true,"writable"=>true,"required"=>true],
            ["name"=>"images",  "title"=>"多张图片", "type"=>"images",   "value"=>[],                  "describe"=>"请上传图片",          "readable"=>true,"writable"=>true,"required"=>true],
            ["name"=>"file",    "title"=>"文件",    "type"=>"file",      "value"=>"",                  "describe"=>"请上传文件",          "readable"=>true,"writable"=>true,"required"=>true],
            ["name"=>"image",   "title"=>"图片",    "type"=>"image",     "value"=>"",                  "describe"=>"请上传图片",          "readable"=>true,"writable"=>true,"required"=>true],
        ];
        return $config;
    }
    protected function load()
    {
        // $config=$this->loadConfig();
        // $config=ConfigFileModel::alloc()->load();
        $config=ConfigTableModel::alloc()->load();
        return $config;
    }
    public function showForm()
    {
        $widget=FormWidget::alloc()->setAction(url("update"))->setFields($this->load())->fetch();
        $this->assign("widget",$widget);
        return $this->fetch("frame");
    }
    public function update()
    {
        try {
            $data=input();
            $config=FormWidget::alloc()->setFields($this->load())->input($data)->getFields();

            // ConfigFileModel::alloc()->save($config);
            // ConfigTableModel::alloc()->create($config,"test");
            ConfigTableModel::alloc()->updateValue($config);

            $this->success("操作成功");
        } catch (Exception $exce) {
            print_r($exce);
            exit;
        }
    }
}
